import http from "../../../utils/http.js"
import reg from "../../../utils/regular.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone:"",//手机号
        oldpassword:"",//旧密码
        newpassword:""//新密码
    },

    /**
     * 生命周期函数--监听页面加载
     */
    phoneblur(  ){
      let regular = reg.regular(this.data.phone)
      if(!regular){
            wx.showToast({
              title: '请输入正确的手机号',
              icon:"none",
            })
            return
      }
    },
    onLoad: function (options) {

    },
    //旧密码失焦
    oldpassblur(){
      let regular = reg.Regpassword(this.data.oldpassword)
      if(!regular){
        wx.showToast({
          title: '密码必须是大小字母数字特殊组成(8位)',
          icon:"none"
        })
        return
      }
    },    //新密码失焦
    newpassblur(){
      let regular = reg.Regpassword(this.data.newpassword)
      if(!regular){
        wx.showToast({
          title: '新密码必须是大小字母数字特殊组成(8位)',
          icon:"none"
        })
        return
      }
    },
        //重置密码(){
   async resetPassword(){
        let {phone,oldpassword,newpassword} = this.data 
        if(oldpassword == "" ||  newpassword == '' || phone == '' ){
          wx.showToast({
            title: "所填信息不能为空",
            icon:"none"
          })
          return
        }else if(oldpassword == newpassword){
          wx.showToast({
            title: '新旧密码不能相同',
          })
          return
        }else{
          let userInfo = wx.getStorageSync('user')
          let option = {
            url:"/user/update/password",
            data:{
              userId:userInfo.user.id,
              token:userInfo.token,
              password:oldpassword,
              newPassword:newpassword
            }
          }
          let  res = await http(option)
          if(res.data.status == 200){
            wx.showToast({
              title: '修改成功',
            })
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1,
              })
            },1500)
          }else if(res.data.status == 400){
            wx.showToast({
              title: res.data.msg,
              icon:"none"
            })
            return
          }
        }
    },
    //忘记密码
   async  forgetPass(){
     let {phone} = this.data
      let option = {
         url:"/user/forget/msgcode",
         data:{
          telephone:phone,
          timestamp:1519+Math.floor(Math.sqrt(phone))+'.0'
         }
      }
      let res = await http(option)
      console.log(res);
    },
        
})